package com.pirestupppai.ui.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.blog.www.guideview.Component;
import com.blog.www.guideview.Guide;
import com.blog.www.guideview.GuideBuilder;
import com.pirestupppai.R;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.common.Constant;
import com.pirestupppai.imagetool.mattingtool.MattingView;
import com.pirestupppai.utils.GuideComponent;
import com.pirestupppai.utils.SharedPreferenceUtil;
import com.pirestupppai.widgets.dialog.ProgressingDialog;

import butterknife.BindView;

/**
 * 抠图页面
 */

public class ImageMattingActivity extends BaseActivity {

    @BindView(R.id.matting_view)
    MattingView mattingView;
    @BindView(R.id.complete_btn)
    ImageView mCompleteBtn;
    @BindView(R.id.back_btn)
    ImageView mBackBtn;
    @BindView(R.id.paint_layout)
    RelativeLayout mMattingPaintLayout;
    @BindView(R.id.eraser_layout)
    RelativeLayout mEraserPaintLayout;
    @BindView(R.id.paint_btn)
    ImageView mPaintBtn;
    @BindView(R.id.eraser_btn)
    ImageView mEraserBtn;

    private Guide mGuideOperation;
    private ProgressingDialog mDialog;

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected void initListeners() {
        mCompleteBtn.setOnClickListener(this);
        mMattingPaintLayout.setOnClickListener(this);
        mEraserPaintLayout.setOnClickListener(this);
        mBackBtn.setOnClickListener(this);
    }

    @Override
    protected void initViews() {
        mPaintBtn.setSelected(true);
        mDialog = new ProgressingDialog(mContext);
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_image_matting;
    }

    @Override
    protected void initLogic() {
        mattingView.setBackgroundBitmap(ImageEditActivity.mDeliverBitmap);

        SharedPreferences sp = SharedPreferenceUtil.getSp();
        SharedPreferences.Editor editor = SharedPreferenceUtil.getEditor();

        boolean isFirstRun = sp.getBoolean(Constant.SPKey.FIRSTIMAGEMATTINGACTIVITY, true);
        if (isFirstRun) {
            editor.putBoolean(Constant.SPKey.FIRSTIMAGEMATTINGACTIVITY, false);
            editor.apply();
            // 引导层
            mPaintBtn.post(new Runnable() {
                @Override
                public void run() {
                    ShowPaintGuideView();
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.complete_btn:
                mDialog.show();
                // 开启子线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ImageEditActivity.mDeliverBitmap = mattingView.getBgBitmap();
                        setResult(RESULT_OK);
                        mDialog.dismiss();
                        ImageMattingActivity.this.finish();
                    }
                }).start();
                break;
            case R.id.back_btn:
                this.finish();
                break;
            case R.id.paint_layout:
                mattingView.SetMattingStatus(true);
                mPaintBtn.setSelected(true);
                mEraserBtn.setSelected(false);
                break;
            case R.id.eraser_layout:
                mattingView.SetMattingStatus(false);
                mPaintBtn.setSelected(false);
                mEraserBtn.setSelected(true);
                break;
        }
    }

    public void ShowPaintGuideView() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(mPaintBtn)
                .setAlpha(150)
                .setHighTargetCorner(20)
                .setHighTargetPaddingLeft(25)
                .setHighTargetPaddingRight(25)
                .setHighTargetPaddingBottom(25)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder.setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
            @Override
            public void onShown() {
            }

            @Override
            public void onDismiss() {
                ShowEraserGuideView();
            }
        });
        builder.addComponent(new GuideComponent(mContext, "用画笔涂抹希望保留的部分", Component.ANCHOR_TOP, Component.FIT_START, -50, -50));
        mGuideOperation = builder.createGuide();
        mGuideOperation.setShouldCheckLocInWindow(false);
        mGuideOperation.show(ImageMattingActivity.this);
    }

    public void ShowEraserGuideView() {
        GuideBuilder builder = new GuideBuilder();
        builder.setTargetView(mEraserBtn)
                .setAlpha(150)
                .setHighTargetCorner(20)
                .setHighTargetPaddingLeft(20)
                .setHighTargetPaddingRight(20)
                .setHighTargetPaddingBottom(25)
                .setOverlayTarget(false)
                .setOutsideTouchable(false);
        builder.addComponent(new GuideComponent(mContext, "用橡皮擦擦除选中区域", Component.ANCHOR_TOP, Component.FIT_END, 50, -50));
        mGuideOperation = builder.createGuide();
        mGuideOperation.setShouldCheckLocInWindow(false);
        mGuideOperation.show(ImageMattingActivity.this);
    }
}
